package com.uplift.mobile.product.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper;
import com.uplift.mobile.product.model.Order;
import com.uplift.mobile.product.service.IOrderService;

/**
 * 订单相关
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/order")
public class OrderController {

	@Autowired
	private IOrderService orderService;

	/**
	 * 查询订单
	 * 
	 * @param order
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(HttpServletRequest request, Order order) {
		try {
			List<Map<String, Object>> list = this.orderService.queryOrder(order);
			return ResultWrapper.wrapListPage(list, ResultWrapper.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper.wrapListPage(null, ResultWrapper.FAIL, "查询失败");
		}

	}

	/**
	 * 新增订单
	 * 
	 * @param request
	 * @param order
	 * @return IResultable
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public IResultable save(HttpServletRequest request, Order order) {
		try {
			int row = this.orderService.saveOrder(order);
			if (row > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "订单提交成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "订单提交失败");
			}
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "订单提交失败");
		}
	}

	/**
	 * 修改订单
	 * 
	 * @param request
	 * @param order
	 * @return IResultable
	 */
	@RequestMapping(value = "/modify")
	@ResponseBody
	public IResultable modify(HttpServletRequest request, Order order) {
		try {
			int row = this.orderService.modifyOrder(order);
			if (row > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "修改成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "修改失败");
			}
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "修改失败");
		}
	}

	/**
	 * 删除订单
	 * 
	 * @param request
	 * @param Order
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	@ResponseBody
	public IResultable remove(HttpServletRequest request, Order order) {
		try {
			int row = this.orderService.removeOrder(order);
			if (row > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "删除成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "删除失败");
			}
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "删除失败");
		}
	}
}
